package com.byit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.domain.AppAddress;
import com.byit.domain.AppApplication;
import com.byit.exception.ServiceException;
import com.byit.mapper.AppAddressMapper;
import com.byit.service.IAppAddressService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.byit.utils.PageUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 常用地址表 服务实现类
 * </p>
 *
 * @author puyunfeng
 * @since 2019-01-17
 */
@Service
public class AppAddressServiceImpl extends ServiceImpl<AppAddressMapper, AppAddress> implements IAppAddressService {

    @Override
    public List<JSONObject> selectByUserId(Long userId) {
        List<JSONObject> list = this.baseMapper.selectByUserId(userId);
        return list;
    }

    @Override
    public void saveOrUpdate(AppAddress address) throws ServiceException {

        if(address.getId() == 0){
            address.setCreateTime(new Date());
            address.setUpdateTime(new Date());
        } else {
            address.setUpdateTime(new Date());
        }

        // 更新地址全称
        JSONObject area = this.baseMapper.selectArea(address.getAreaId());
        if(area == null){
            throw new ServiceException("areaId未得到");
        }
        address.setFullArea(area.getString("fullName"));


        // 判断是否设置默认地址做更新
        if(address.getIsDefault() == 1){
            // 用户的默认地址全部刷为0
            this.baseMapper.updateNoDefault(address.getUserId());
/*
            // 将当前刷为默认地址
            MemberAddress params = new MemberAddress();
            params.setIsDefault(1);
            params.setUserId(address.getUserId());
            params.setId(address.getId());
            params.setUpdateTime(new Date());
            this.updateById(params);*/
        }

        this.insertOrUpdate(address);
    }

    @Override
    public void deleteByIdAndUserId(Long addressId, Long userId) {
        this.baseMapper.deleteByIdAndUserId(addressId,userId);
    }

    @Override
    public List<JSONObject> selectProvince() {
        List<JSONObject> provinceList = this.baseMapper.selectProvince();
        return  provinceList;
    }

    @Override
    public List<JSONObject> selectCity(Long provinceId) {
        List<JSONObject> cityList = this.baseMapper.selectCity(provinceId);
        return  cityList;
    }

    @Override
    public List<JSONObject> selectRegion(Long cityId) {
        List<JSONObject> regionList = this.baseMapper.selectRegion(cityId);
        return  regionList;
    }

    @Override
    public Page<JSONObject> selectAddressList(Page<AppApplication> pageInfo, Map<String, Object> param) {
        List<JSONObject> list = this.baseMapper.selectAddressList(pageInfo,param);

        Page<JSONObject> page = PageUtil.transform(pageInfo);

        page.setRecords(list);

        return page;
    }
}
